Listing 2

struct scancodes{
    char code1;
    char code2;
};
extern struct scancodes receive(void);

const unsigned char codes[][2] = 
{0x1c,'a',0x32,'b',0x21,'c',0x23,'d',0x24,'e',0,0};
const unsigned char shifted_codes[][2] = 
{0x1c,'A',0x32,'B',0x21,'C',0x23,'D',0x24,'E',0,0};

void main(void)
{
    // Initialise and clear the LCD
    lcd_init();
    lcd_clear();

    printf("Press a key:");
    // Loop forever
    while(1)
    {
        key = getch();
        lcd_clear();
        printf("You pressed [%c]", key);
    }
}

void putch(char c)
{
    lcd_putch(c);
}

unsigned char table_lookup(const unsigned char array[][2], char code)
{
    unsigned char x;
    for(x = 0; array[x][0]; x++)
        if (array[x][0] == code)
        {
            return(array[x][1]);
        }
    return '?';
}

unsigned char getch(void)
{
    struct scancodes scan;
    static unsigned char shift = 0;
    do
    {
        scan = receive();
        // check for shift keys
        if (scan.code1 == LSHIFT || scan.code1 == RSHIFT)
        {
             shift = (scan.code2 != BREAK);
        }
        else
            break;
    }while(1);

    if(!shift)
        return table_lookup(codes, scan.code1);
    else
        return table_lookup(shifted_codes, scan.code1);
}
